package com.shundr.user;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.shundr.R;
import com.shundr.base.BaseActivity;
import com.shundr.common.util.ProgressDialogHelper;
import com.shundr.common.util.ToastHelper;
import com.shundr.frame.http.LHttpRequest;
import com.shundr.frame.util.StringUtil;
import com.shundr.user.http.UserRequest;

/**
 * 修改密码
 * ChangePasswordActivity
 * 
 * @author larry 2015-5-28 上午9:05:37 
 * @version 1.0.0
 *
 */
public class ChangePasswordActivity extends BaseActivity implements
		OnClickListener {

	private View vVerifyOldPassword;
	private View vSetNewPassword;
	private EditText etOldPassword;
	private Button btnClearOldPassword;
	private Button btnNextStep;

	private EditText etPassword1;
	private EditText etPassword2;
	private Button btnClear1;
	private Button btnClear2;
	private Button btnSetNewp;
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UserRequest.GET_CONFIRM_USERINFO_SUCCESSED:
//				ToastHelper.toastShort(mContext, msg.obj.toString());
				ProgressDialogHelper.dismiss();
				vVerifyOldPassword.setVisibility(View.GONE);
				vSetNewPassword.setVisibility(View.VISIBLE);
				break;
			case UserRequest.RESER_USER_OLDPSW_SUCCESSED:
				ToastHelper.toastShort(mContext, msg.obj.toString());
				ProgressDialogHelper.dismiss();
				finish();
				break;
			case LHttpRequest.REQUEST_FAILED:
				ProgressDialogHelper.dismiss();
				ToastHelper.toastShort(mContext, msg.obj.toString());
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_passwod);
		vVerifyOldPassword = (View) findViewById(R.id.layout_verify_old_password);
		vSetNewPassword = (View) findViewById(R.id.layout_set_new_password);
		etOldPassword = (EditText) findViewById(R.id.et_old_password);
		btnClearOldPassword = (Button) findViewById(R.id.btn_old_password_clear);
		btnNextStep = (Button) findViewById(R.id.btn_next_step);

		btnClearOldPassword.setOnClickListener(this);
		btnNextStep.setOnClickListener(this);

		etPassword1 = (EditText) findViewById(R.id.et_account1);
		etPassword2 = (EditText) findViewById(R.id.et_account2);
		btnClear1 = (Button) findViewById(R.id.btn_password_clear1);
		btnClear2 = (Button) findViewById(R.id.btn_password_clear2);
		btnSetNewp = (Button) findViewById(R.id.set_new_psd_request);

		btnClear1.setOnClickListener(this);
		btnClear2.setOnClickListener(this);
		btnSetNewp.setOnClickListener(this);
		textChange();
	}

	private void textChange() {
		etOldPassword.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (s.length() > 0) {
					btnClearOldPassword.setVisibility(View.VISIBLE);
				} else {
					btnClearOldPassword.setVisibility(View.GONE);
				}
			}
		});

		etPassword1.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (s.length() > 0) {
					btnClear1.setVisibility(View.VISIBLE);
				} else {
					btnClear1.setVisibility(View.GONE);
				}
			}
		});

		etPassword2.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (s.length() > 0) {
					btnClear2.setVisibility(View.VISIBLE);
				} else {
					btnClear2.setVisibility(View.GONE);
				}

			}
		});
	}

	private void checkOldPassword() {

		if (StringUtil.isNull(etOldPassword.getText().toString())) {
//			ToastHelper.toastShort(mContext, "请输入6到16位非空格密码");
			ToastHelper.toastShort(mContext, "请输入密码");
			etOldPassword.setText("");
			return;
		}
//		if (etOldPassword.getText().toString().length() < 6) {
//			ToastHelper.toastShort(mContext, "输入的密码长度太短,请重新输入");
//			return;
//		}
//		if (etOldPassword.getText().toString().length() > 16) {
//			ToastHelper.toastShort(mContext, "输入的密码长度太长,请重新输入");
//			return;
//		}
		ProgressDialogHelper.show(mContext, "正在验证密码...");
		new UserRequest(mContext, mHandler).getConfirmUserInfo(etOldPassword.getText().toString().trim());
	}

	private void changePassword() {
		if (StringUtil.isNull(etPassword1.getText().toString())) {
			ToastHelper.toastShort(mContext, "请输入6到16位非空格密码");
			etPassword1.setText("");
			return;
		}
		if (etPassword1.getText().toString().length() < 6) {
			ToastHelper.toastShort(mContext, "输入的密码长度太短,请重新输入");
			return;
		}
		if (etPassword1.getText().toString().length() > 16) {
			ToastHelper.toastShort(mContext, "输入的密码长度太长,请重新输入");
			return;
		}
		if (!StringUtil.isEqual(etPassword1.getText().toString(), etPassword2
				.getText().toString())) {
			ToastHelper.toastShort(mContext, "密码不一致,请重新输入");
			etPassword2.setText("");
			return;
		}
		ProgressDialogHelper.show(mContext, "正在重置密码...");
		new UserRequest(mContext, mHandler).resetUserOldPsw(etPassword1.getText().toString().trim());
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_old_password_clear:
			etOldPassword.setText("");
			break;
		case R.id.btn_next_step:
			checkOldPassword();
			break;
		case R.id.btn_password_clear1:
			etPassword1.setText("");
			break;
		case R.id.btn_password_clear2:
			etPassword2.setText("");
			break;
		case R.id.set_new_psd_request:
			changePassword();
			break;
		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		if (vSetNewPassword.isShown()) {
			vSetNewPassword.setVisibility(View.GONE);
			vVerifyOldPassword.setVisibility(View.VISIBLE);
		} else {
			finish();
		}
	}

}
